﻿layui.use(['table', 'element', 'form', 'laydate'], function () {
    var table = layui.table
        , $ = layui.jquery
        , element = layui.element
        , laydate = layui.laydate
        , form = layui.form;

    $("#AddBook").on('click', function () {
        layer.open({
            type: 2,
            title: '添加图书信息',
            shadeclose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['400px', '600px'],
            content: "/bgadmin/Add/AddBook.aspx"
        });
    })

    $("#AddUser").on('click', function () {
        layer.open({
            type: 2,
            title: '添加用户信息',
            shadeclose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['400px', '450px'],
            content: "/bgadmin/Add/AddUser.aspx"
        });
    })

    $("#AddOrder").on('click', function () {
        layer.open({
            type: 2,
            title: '添加订单信息',
            shadeclose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['400px', '300px'],
            content: "/bgadmin/Add/AddOrder.aspx"
        });
    })

    //监听工具条
    table.on('tool(userbar)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            layer.confirm('确定更新该行数据', function (index) {
                $.ajax({
                    type: 'POST',
                    url: "/bgadmin/API/AdminHandler.ashx",
                    data: { TableName: "Users", MethodName: "Update", Entity: JSON.stringify(data) },
                    success: function (responseData) {
                        if (responseData == "true") {
                            layer.msg('更新成功');
                        } else {
                            layer.msg('更新失败,请稍后重试');
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {//请求失败处理函数
                        layer.msg('更新失败,请稍后重试');
                    }
                });
            });
        } else if (obj.event === 'del') {
            layer.confirm('确定删除该记录', function (index) {
                $.ajax({
                    type: 'POST',
                    url: "/bgadmin/API/AdminHandler.ashx",
                    data: { TableName: "Users", MethodName: "Delete", Entity: JSON.stringify(data) },
                    dataType: 'text',
                    async: false,
                    success: function (responseData) {
                        if (responseData == "true") {
                            layer.msg('删除成功');
                            obj.del();
                            layer.close(index);
                        } else {
                            layer.msg('删除失败,请稍后重试');
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {//请求失败处理函数
                        layer.msg('删除失败,请稍后重试');
                    }
                });
            });
        }
    });

    table.on('tool(bookbar)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            layer.confirm('确定更新该行数据', function (index) {
                $.ajax({
                    type: 'POST',
                    url: "/bgadmin/API/AdminHandler.ashx",
                    data: { TableName: "Books", MethodName: "Update", Entity: JSON.stringify(data) },
                    async: false,
                    success: function (responseData) {
                        if (responseData == "true") {
                            layer.msg('更新成功');
                        } else {
                            layer.msg('更新失败,请稍后重试');
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {//请求失败处理函数
                        layer.msg('更新失败,请稍后重试');
                    }
                });
            });
        } else if (obj.event === 'del') {
            layer.confirm('确定删除该记录', function (index) {
                $.ajax({
                    type: 'POST',
                    url: "/bgadmin/API/AdminHandler.ashx",
                    data: { TableName: "Books", MethodName: "Delete", Entity: JSON.stringify(data) },
                    dataType: 'text',
                    async: false,
                    success: function (responseData) {
                        if (responseData == "true") {
                            layer.msg('删除成功');
                            obj.del();
                            layer.close(index);
                        } else {
                            layer.msg('删除失败,请稍后重试');
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {//请求失败处理函数
                        layer.msg('删除失败,请稍后重试');
                    }
                });
            });
        }
    });

    table.on('tool(orderbar)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            layer.confirm('确定更新该行数据', function (index) {
                $.ajax({
                    type: 'POST',
                    url: "/bgadmin/API/AdminHandler.ashx",
                    data: { TableName: "Orders", MethodName: "Update", Entity: JSON.stringify(data) },
                    async: false,
                    success: function (responseData) {
                        if (responseData == "true") {
                            layer.msg('更新成功');
                        } else {
                            layer.msg('更新失败,请稍后重试');
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {//请求失败处理函数
                        layer.msg('更新失败,请稍后重试');
                    }
                });
            });
        } else if (obj.event === 'del') {
            layer.confirm('确定删除该记录', function (index) {
                $.ajax({
                    type: 'POST',
                    url: "/bgadmin/API/AdminHandler.ashx",
                    data: { TableName: "Orders", MethodName: "Delete", Entity: JSON.stringify(data) },
                    dataType: 'text',
                    async: false,
                    success: function (responseData) {
                        if (responseData == "true") {
                            layer.msg('删除成功');
                            obj.del();
                            layer.close(index);
                        } else {
                            layer.msg('删除失败,请稍后重试');
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {//请求失败处理函数
                        layer.msg('删除失败,请稍后重试');
                    }
                });
            });
        }
    });
});